const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentId: '1',
    section: [{
      name: '周统计',
      typeId: '1'
    }, {
      name: '月统计',
      typeId: '2'
    } ,
    {
      name: '年统计',
      typeId: '3'
    }],
    weekStartTime: '', // 本周开始时间
    weekEndTime: '', // 本周结束时间
    monthStartTime: '', // 本月开始时间
    monthEndTime: '', // 本月结束时间
    yearStartTime: '', // 本年开始时间
    yearEndTime: '', // 本年结束时间
    thisWeekData: [], // 本周打卡数据
    thisMonthData: [], // 本月打卡数据
    thisYearData: [], // 本年打卡数据
    fontSetSize: 'small', // 用户设置的字体大小（'small'：小，'middle'：中，'large'：大）
    type:""
  },

  /**
   * 切换区间
   * @param {*} e 
   */
  handleTap: function(e) {
    let id = e.currentTarget.id;
    if(id){
      this.setData({
        currentId:id
      })
    }
  },

  /**
   * 获取本周打卡统计
   */
  getWeekPunchTheClock () {
    let url = "";
    switch(this.data.type*1){
      case 1:
        url = "/api/User/company_read_work_record"
        break;
      case 2:
        url = "/api/User/personal_read_work_record"
        break;
      case 3:
        url = "/api/User/company_work_record"
    }
    app.http({
      config: {
        url,
        data: {
          start_time: this.data.weekStartTime,
          end_time: this.data.weekEndTime
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'thisWeekRes');
      let thisWeekData = res.data.data
      let _user = thisWeekData.user
      _user.forEach(item => {
        let _created_at = item.created_at ? item.created_at.substr(0, 10) : item.created_at
        item.created_at = _created_at
      })
      thisWeekData.user = _user
      console.log(thisWeekData)
      this.setData({
        thisWeekData
      })
    }).catch(err => {
      console.log(err, 'thisWeekErr')
    })
  },

  /**
   * 获取本月打卡统计
   */
  getMonthPunchTheClock () {
    let url = "";
    switch(this.data.type*1){
      case 1:
        url = "/api/User/company_read_work_record"
        break;
      case 2:
        url = "/api/User/personal_read_work_record"
        break;
      case 3:
        url = "/api/User/company_work_record"
    }
    app.http({
      config: {
        url ,
        data: {
          start_time: this.data.monthStartTime,
          end_time: this.data.monthEndTime
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'thisMonthRes');
      let thisMonthData = res.data.data
      let _user = thisMonthData.user
      _user.forEach(item => {
        let _created_at = item.created_at ? item.created_at.substr(0, 10) : item.created_at
        item.created_at = _created_at
      })
      thisMonthData.user = _user
      console.log(thisMonthData)
      this.setData({
        thisMonthData
      })
    }).catch(err => {
      console.log(err, 'thisMonthErr')
    })
  },

  /**
   * 获取本年打卡统计
   */
  getYearPunchTheClock () {
    let url = "";
    switch(this.data.type*1){
      case 1:
        url = "/api/User/company_read_work_record"
        break;
      case 2:
        url = "/api/User/personal_read_work_record"
        break;
      case 3:
        url = "/api/User/company_work_record"
    }
    app.http({
      config: {
        url ,
        data: {
          start_time: this.data.yearStartTime,
          end_time: this.data.yearEndTime
        },
        method: 'POST'
      },
      isAuth: true
    }).then(res => {
      console.log(res, 'thisYearRes');
      let thisYearData = res.data.data
      let _user = thisYearData.user
      _user.forEach(item => {
        let _created_at = item.created_at ? item.created_at.substr(0, 10) : item.created_at
        item.created_at = _created_at
      })
      thisYearData.user = _user
      console.log(thisYearData)
      this.setData({
        thisYearData
      })
    }).catch(err => {
      console.log(err, 'thisYearErr')
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type:options.type
    })

    console.log(this.data.type , "this home")
    let date = new Date()
    let year = date.getFullYear()
    let month = date.getMonth()
    let day = date.getDay()
    let timeStamp = new Date().getTime() // 当前时间戳

    let thisWeekStartDate = new Date((timeStamp - (1000 * 60 * 60 * 24 * day)))
    let thisWeekEndDate = new Date()

    let thisWeekStartTimeYear = thisWeekStartDate.getFullYear()
    let thisWeekStartTimeMonth = thisWeekStartDate.getMonth() + 1
    thisWeekStartTimeMonth = thisWeekStartTimeMonth < 10 ? ('0' + thisWeekStartTimeMonth) : thisWeekStartTimeMonth
    let thisWeekStartTimeDate = thisWeekStartDate.getDate()
    thisWeekStartTimeDate = thisWeekStartTimeDate < 10 ? ('0' + thisWeekStartTimeDate) : thisWeekStartTimeDate
    
    let thisWeekEndTimeYear = thisWeekEndDate.getFullYear()
    let thisWeekEndTimeMonth = thisWeekEndDate.getMonth() + 1
    thisWeekEndTimeMonth = thisWeekEndTimeMonth < 10 ? ('0' + thisWeekEndTimeMonth) : thisWeekEndTimeMonth
    let thisWeekEndTimeDate = thisWeekEndDate.getDate()
    thisWeekEndTimeDate = thisWeekEndTimeDate < 10 ? ('0' + thisWeekEndTimeDate) : thisWeekEndTimeDate
 
    // 获取今年最后一个月的最后一天（单位：号，例如30号）
    // console.log(new Date(year + 1, 0, 0).getDate())
    let thisYearLastDate = new Date(year + 1, 0, 0).getDate()
    
    // 获取本月的最后一天（单位：号，例如30号）
    // console.log(new Date(year, month + 1, 0).getDate())
    let thisMonthLastDate = new Date(year, month + 1, 0).getDate()
    
    let weekStartTime = `${thisWeekStartTimeYear}-${thisWeekStartTimeMonth}-${thisWeekStartTimeDate}`
    let weekEndTime = `${thisWeekEndTimeYear}-${thisWeekEndTimeMonth}-${thisWeekEndTimeDate}`
    let monthStartTime = month < 9 ? `${year}-0${month + 1}-01` : `${year}-${month + 1}-01`
    let monthEndTime = month < 9 ? `${year}-0${month + 1}-${thisMonthLastDate}` : `${year}-${month + 1}-${thisMonthLastDate}`
    let yearStartTime = `${year}-01-01`
    let yearEndTime = `${year}-12-${thisYearLastDate}`

    console.log(weekStartTime, weekEndTime)
    console.log(monthStartTime, monthEndTime)
    console.log(yearStartTime, yearEndTime)
    
    this.setData({
      weekStartTime,
      weekEndTime,
      monthStartTime,
      monthEndTime,
      yearStartTime,
      yearEndTime,
    })
    this.getWeekPunchTheClock()
    this.getMonthPunchTheClock()
    this.getYearPunchTheClock()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let fontSetSize = wx.getStorageSync('fontSetSize') // 本地缓存中用户设置的字体大小
    if (fontSetSize) {
      this.setData({
        fontSetSize
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})